<?php
/**
 * 我的项目
 */
namespace app\model;

use think\Model;
use app\model\Chain;
use app\model\CompanyModel;

class ProjectsModel extends Model
{
	protected $name = 'projects';
	/**
	 * 我的项目列表
	 *
	 */
	public function GetList($where){
		$ProjectsModel=new ProjectsModel;
		$result=$ProjectsModel->field('id,name,company_name,identification,address,updatetime,status,from,is_monitor')->where($where)->order('id desc')->paginate()->toArray();
		foreach ($result['data'] as &$val){
			$val['updatetime']=date('Y-m-d H:i:s',$val['updatetime']);
		}
		return $result;
	}
	/**
	 * 我的项目详情
	 *
	 */
	public function detail($id){
		$ProjectsModel = ProjectsModel::find($id);
		return $ProjectsModel;
	}
	/**
	 * 添加
	 *
	 */
	public function AddData($param){
		$ProjectsModel = new ProjectsModel;
		$param['createtime']=time();
		$param['updatetime']=time();
		if(isset($param['company_name'])&&!empty($param['company_name'])){
			$CompanyModel=new CompanyModel();
			$param['company_name']=trim($param['company_name']);
			$param['identification']=$CompanyModel->GetCompanyIdentificationByName(trim($param['company_name']));
		} 
		
		/* if(isset($param['chain_id'])&&!empty($param['chain_id'])){
			 $ChainModel=new Chain();
			 $ids=explode(',', $param['chain_id']);
			 $chain_id=[];
			 foreach ($ids as $val){
			   $chain_id[]=$ChainModel->GetChainIdById($val);
			 }
			 $param['chain_id']=implode(',', $chain_id);
		} */
		$field=['name','company_name','identification','desc','address','content','createtime','updatetime','user_id','chain_id','from','status'];
		
		$ProjectsModel->allowField($field)->save($param);		
		$id=$ProjectsModel->id;		
		return $id;
	}
	/**
	 * 编辑
	 *
	 */
	public function EditData($param,$id,$field=[]){
		$ProjectsModel = ProjectsModel::find($id);
		$param['updatetime']=time();
		if(isset($param['company_name'])&&!empty($param['company_name'])){
			$CompanyModel=new CompanyModel();
			$param['identification']=$CompanyModel->GetCompanyIdentificationByName($param['company_name']);
		} 
		/* if(isset($param['chain_id'])&&!empty($param['chain_id'])){
			 $ChainModel=new Chain();
			 $ids=explode(',', $param['chain_id']);
			 $chain_id=[];
			 foreach ($ids as $val){
			 $chain_id[]=$ChainModel->GetChainIdById($val);
			 }
			 $param['chain_id']=implode(',', $chain_id);
		 } 
		 */
		if(empty($field)){
			$field=['name','company_name','identification','desc','address','content','updatetime','chain_id','status'];			
		}	
		$ProjectsModel->allowField($field)->save($param);
		
		return $id;
	}
	/**
	 * 修改禁用状态
	 *
	 */
	public function UpdateStatus($id){
		$ProjectsModel = ProjectsModel::find($id);
		$ProjectsModel->status=$ProjectsModel->status==1?0:1;
		$ProjectsModel->save();
		return true;
	}
	/**
	 * 删除
	 *
	 */
	public function Del($param){
		$update=[];
		foreach($param as $val){
			$update[]=['id'=>$val,'status'=>-1,'deletetime'=>time()];
		}
		if(!empty($update)){
			$ProjectsModel = new ProjectsModel;
			$ProjectsModel->saveAll($update);
		}
		return true;
	}
	/**
	 * 项目状态
	 */
	public function GetProjectTypeById($id){
		$data=[
				['id'=>1,'name'=>'新建项目'],
				['id'=>2,'name'=>'在跟项目'],
				['id'=>3,'name'=>'签约项目'],
				['id'=>4,'name'=>'前期准备'],
				['id'=>5,'name'=>'开工项目'],
				['id'=>6,'name'=>'投产项目'],
				['id'=>7,'name'=>'已完成'],
		];
		foreach ($data as $val){
			if($val['id']==$id){
				return $val['name'];
			}
		}
		return '';
	}
}